package com.foreknow.service.impl;

import com.foreknow.bean.Guestbook;
import com.foreknow.dao.GuestbookDao;
import com.foreknow.dao.impl.GuestbookDaoImpl;
import com.foreknow.db.DBManager;
import com.foreknow.db.JdbcTemplate;
import com.foreknow.service.GuestbookService;
import com.foreknow.util.Pagination;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class GuestbookServiceImpl implements GuestbookService {
    @Override
    public boolean save(Guestbook gb) {
        DBManager dbManager = DBManager.getInstance();
        Connection conn = dbManager.getConnection();
        JdbcTemplate jdbcTemplate = JdbcTemplate.getInstance();
        //将DBManager中的conn传递到JdbcTemplate中的conn,才能对数据库操作
        jdbcTemplate.setConnection(conn);
        GuestbookDao guestbookDao = new GuestbookDaoImpl();
        boolean isRigth = false;
        try {
            isRigth = guestbookDao.addMessage(gb);
            //提交事物
            conn.commit();
        } catch (SQLException e) {
            try {
                conn.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
            e.printStackTrace();
        }
        return isRigth;
    }

    @Override
    public List<Object> getQueryInfo() {
        DBManager dbManager = DBManager.getInstance();
        Connection conn = dbManager.getConnection();
        JdbcTemplate jdbcTemplate = JdbcTemplate.getInstance();
        //将DBManager中的conn传递到JdbcTemplate中的conn,才能对数据库操作
        jdbcTemplate.setConnection(conn);
        GuestbookDao guestbookDao = new GuestbookDaoImpl();
        List<Object> list = null;
        try {
            list = guestbookDao.getQueryAll();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public boolean deleteGuestbookById(Integer id) {
        DBManager dbManager = DBManager.getInstance();
        Connection conn = dbManager.getConnection();
        JdbcTemplate jdbcTemplate = JdbcTemplate.getInstance();
        //将DBManager中的conn传递到JdbcTemplate中的conn,才能对数据库操作
        jdbcTemplate.setConnection(conn);
        GuestbookDao guestbookDao = new GuestbookDaoImpl();
        boolean isRigth = false;
        try {
            isRigth = guestbookDao.delById(id);
            //提交事物
            conn.commit();
        } catch (SQLException e) {
            try {
                conn.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
            e.printStackTrace();
        }
        return isRigth;
    }
    @Override
    public Pagination getByPage(int pageSize, int pageNumber) {
        DBManager dbManager = DBManager.getInstance();
        Connection connection = dbManager.getConnection();
        //要将DBManager的connection注入到JdbcTemplate中
        //实例化dao并调用dao中的登录方法
        GuestbookDaoImpl  daoImpl=new GuestbookDaoImpl();
        daoImpl.setConnection(connection);
        Pagination pagination = null;
        try {
            pagination = daoImpl.getGuestbookByPage(pageSize, pageNumber);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return pagination;
    }
}
